<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: ImagePortlet.php 460 2011-01-16 11:45:11Z steen.rabol $
 * @package Pradoportal.Porttlets.ImagePortlet.Edit
 *
 */

Prado::using('Application.Portlets.Portlet');
Prado::using('Application.Common.Data.PortalConfigRecord');
Prado::using('Application.Common.Data.PortalPageRecord');

class ImagePortlet extends Portlet
{
	private $_portletconfig;

	public function getPortletConfig()
	{
		return $this->_portletconfig;
	}

	protected function getConfigName()
	{
		return get_class() . '_' . $this->Parameters['id'];
	}

	public function onInit($param)
	{
		parent::onInit($param);

		$cRow	= $this->ConfigRecord;
		if(!$cRow)
		{
			$value			= serialize(array(	"imagename"			=> "",
												"imagewidth"		=> "",
												"imageheight"		=> "",
												"imageborder"		=> "",
												"imagebordercolor"	=> "",
												"cssclass"			=> "",
												"imageurl"			=> "",
												"cssextra"			=> "",
												"boxcss"			=> "imageportlet",
												"boxcssextra"		=> "",
												"page"				=> ""));
			$cRow			= new PortalConfigRecord();
			$cRow->name		= $this->ConfigName;
			$cRow->value	= $value;
			$cRow->save();
		}

		$this->_portletconfig		= unserialize($cRow->value);
		$this->_portletconfig['id']	= $cRow->id;

		if(!isset($this->_portletconfig['cssextra']))
			$this->_portletconfig['cssextra'] = '';

		if(!isset($this->_portletconfig['boxcss']))
			$this->_portletconfig['boxcss'] = 'imageportlet';

		if(!isset($this->_portletconfig['boxcssextra']))
			$this->_portletconfig['boxcssextra'] = '';

		if(!isset($this->_portletconfig['imagename']))
			$this->_portletconfig['imagename'] = '';

		if(!isset($this->_portletconfig['imagewidth']))
			$this->_portletconfig['imagewidth'] = '';

		if(!isset($this->_portletconfig['imageheight']))
			$this->_portletconfig['imageheight'] = '';

		if(!isset($this->_portletconfig['imageborder']))
			$this->_portletconfig['imageborder'] = '';

		if(!isset($this->_portletconfig['imagebordercolor']))
			$this->_portletconfig['imagebordercolor'] = '';

		if(!isset($this->_portletconfig['imageurl']))
			$this->_portletconfig['imageurl'] = '';

		// Set validation for imagename
		$this->ipImageVal->ValidationGroup	= "ipImage_" . $this->Parameters['id'];
		$this->SaveImage->ValidationGroup	= "ipImage_" . $this->Parameters['id'];

		// Set Validation group for Upload
		$this->ipImageUploadVal->ValidationGroup	= "ipImageUpload_" . $this->Parameters['id'];
		$this->IpImageFile->ValidationGroup			= "ipImageUpload_" . $this->Parameters['id'];

		// Load pages
		$this->ImagePage->DataSource = $this->getPages();
		if(isset($this->_portletconfig['page']) && $this->_portletconfig['page'] != '')
			$this->ImagePage->SelectedValue = $this->_portletconfig['page'];
		$this->ImagePage->dataBind();
	}

	public function dataBind()
	{
		$this->ImageCssClass->Text		= TPropertyValue::ensureString(
													(isset(
															$this->PortletConfig['cssclass']) ?
															$this->PortletConfig['cssclass'] :
															""));
		$this->ImageWidth->Text			= TPropertyValue::ensureString($this->PortletConfig['imagewidth']);
		$this->ImageHeight->Text		= TPropertyValue::ensureString($this->PortletConfig['imageheight']);
		$this->ImageBorder->Text 		= TPropertyValue::ensureString($this->PortletConfig['imageborder']);
		$this->ImageBorderColor->Text	= TPropertyValue::ensureString($this->PortletConfig['imagebordercolor']);
		$this->ImageUrl->Text			= TPropertyValue::ensureString($this->PortletConfig['imageurl']);
		$this->ImageCssExtra->Text		= TPropertyValue::ensureString($this->PortletConfig['cssextra']);

		$this->ImageDivCssClass->Text		= TPropertyValue::ensureString($this->PortletConfig['boxcss']);
		$this->ImageDivCssExtra->Text		= TPropertyValue::ensureString($this->PortletConfig['boxcssextra']);

		$imagefilespath	= $this->Application->PortalBasePath . '/storage/images';
		$imagefiles = PortalUtil::getFilesInPath($imagefilespath,"name");
		try
		{
			$this->ImageName->DataSource	= $imagefiles;
			$this->ImageName->dataBind();
		}
		catch (Exception $e)
		{
			PortalUtil::Log('Add datasource to ImagePortlet: ' . $e->getMessage());
		}
		
		if(is_array($imagefiles))
		{
			foreach($imagefiles as $im)
			{
				 if(in_array($this->PortletConfig['imagename'],$im))
					$this->ImageName->SelectedValue = $this->PortletConfig['imagename'];
			}
		}
	}

	public function onLoad($p)
	{
		parent::onLoad($p);
		if(!$this->Page->isPostBack)
		{
			$this->dataBind();
			// Set the MAX file size to the value in php.ini
			//$this->FileUpload->MaxFileSize	= PortalUtil::return_bytes(ini_get('upload_max_filesize'));
			//$this->ltFileSize->Text			= ini_get('upload_max_filesize');
		}
	}

	public function SaveImage($sender,$param)
	{
		$imagename			= $this->ImageName->SelectedValue;
		$imagewidth			= $this->ImageWidth->Text;
		$imageheight		= $this->ImageHeight->Text;
		$imageborder		= $this->ImageBorder->Text;
		$imagebordercolor	= $this->ImageBorderColor->Text;
		$cssclass			= $this->ImageCssClass->Text;
		$imageurl			= $this->ImageUrl->Text;
		$cssextra			= $this->ImageCssExtra->Text;
		$boxcss				= $this->ImageDivCssClass->Text;
		$boxcssextra		= $this->ImageDivCssExtra->Text;
		$page				= $this->ImagePage->SelectedValue;

		$value				= serialize(array(	"imagename"			=> $imagename,
												"imagewidth"		=> $imagewidth,
												"imageheight"		=> $imageheight,
												"imageborder"		=> $imageborder,
												"imagebordercolor"	=> $imagebordercolor,
												"cssclass"			=> $cssclass,
												"imageurl"			=> $imageurl,
												"cssextra"			=> $cssextra,
												"boxcss"			=> $boxcss,
												"boxcssextra"		=> $boxcssextra,
												"page"				=> $page));

		$this->_portletconfig		= unserialize($value);
		$this->ConfigRecord->value	= $value;
		$this->ConfigRecord->save();
	}

	public function CancelContent($sender,$param)
	{
		$this->dataBind();
	}

	public function DeleteButtonClicked($sender,$param)
	{
		$this->ConfigRecord->delete();
		// Call Parent to do system housekeeping
		parent::DeleteButtonClicked($sender,$param);
	}

	public function onFileUpload($sender,$param)
	{
		if($sender->HasFile)
		{
			$savepath	= str_replace("\\", "/",dirname($this->getRequest()->getApplicationFilePath()));
			$savepath 	.= '/';
			$savepath	.= 'storage/image/';
			$savepath	.= $sender->FileName;

			$sender->saveAs($savepath);
			$imgattr	= getimagesize($savepath);

			$imagefilespath	= $this->Application->PortalBasePath . '/storage/image';
			$imagefiles = PortalUtil::getFilesInPath($imagefilespath,"name");
			$this->ImageName->DataSource	= $imagefiles;
			$this->ImageName->dataBind();

			if(is_array($imagefiles))
			{
				foreach($imagefiles as $im)
				{
					 if(in_array($sender->FileName,$im))
						$this->ImageName->SelectedValue = $sender->FileName;
				}
			}

			$this->ImageWidth->Text		= $imgattr[0];
			$this->ImageHeight->Text	= $imgattr[1];
		}
	}

	public function dlImageNameChanged($sender,$param)
	{
		$item = $sender->Items[$sender->SelectedIndex];
		$imagefilespath	= $this->Application->PortalBasePath . '/storage/images/' .$item->Text;
		if(file_exists($imagefilespath))
		{
			$imgattr	= getimagesize($imagefilespath);
			$this->ImageWidth->Text		= $imgattr[0];
			$this->ImageHeight->Text	= $imgattr[1];
		}

    }

	public function getPages()
	{
		$a = PortalPageRecord::finder()->findAll('order by path');
		$pages = array();
		$pages[] = array('displayname' => Prado::localize('login'), 'fullpath' => 'User.Login','path' => 'User');
		$pages[] = array('displayname' => Prado::localize('Logout'), 'fullpath' => 'User.Logout','path' => 'User');
		$pages[] = array('displayname' => Prado::localize('New user'), 'fullpath' => 'User.NewUser','path' => 'User');
		$pages[] = array('displayname' => Prado::localize('Forgotten password'), 'fullpath' => 'User.ForgottenPassword','path' => 'User');
		$pages[] = array('displayname' => Prado::localize('View user'), 'fullpath' => 'User.ViewUser','path' => 'User');

		foreach($a as $b)
		{
			$pages[] = array('displayname' => $b->displayname, 'fullpath'	=> $b->fullpath, 'path' => $b->path);
		}
		return $pages;
	}

}
?>